from scrapy.commands import ScrapyCommand
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings

from bookcrawl.spiders.amazon import AmazonSpider
from bookcrawl.spiders.dangdang import DangdangSpider
from bookcrawl.spiders.bookschina import BookschinaSpider

class Command(ScrapyCommand):
    requires_project = True

    def syntax(self):
        return '[options]'

    def short_desc(self):
        return 'Runs all of the spiders'

    def add_options(self, parser):
        ScrapyCommand.add_options(self, parser)

    def process_options(self, args, opts):
        ScrapyCommand.process_options(self, args, opts)

    def run(self, args, opts):
        settings = get_project_settings()
        crawl_list = [
            AmazonSpider(),
            BookschinaSpider(),
            DangdangSpider(),
        ]
        process = CrawlerProcess(settings)
        for spider in crawl_list:
            process.crawl(spider)
        process.start()
